#include "analysingthread.h"

AnalysingThread::AnalysingThread(QString str)
{
    this->s = str;
}

int AnalysingThread::getNumOfFucked()
{
    return this->numOfFucked;
}

void AnalysingThread::run()
{
    QFile codeFile(s);

    fixedStringList = new QStringList();

    QRegExp reg("(；|;)");

    if(codeFile.exists())
        if(codeFile.open(QIODevice::ReadWrite | QIODevice::Text))
        {
            QTextStream codeStream(&codeFile);

            codeStream.setAutoDetectUnicode(true);

            while(!codeStream.atEnd()){

                QString s = codeStream.readLine();
                numOfFucked += s.count(reg);
                QString temp = s.replace(reg, ";");
                fixedStringList->insert(fixedStringList->size(), temp);
            }

          codeFile.close();

          writeString();
        }
}

void AnalysingThread::writeString()
{
    QFile fixedFile(s);

    if(fixedFile.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream saveStream(&fixedFile);

        saveStream.autoDetectUnicode();

        if(!fixedStringList->isEmpty()){

            qDebug() << fixedStringList;

            for(int i = 0;i < fixedStringList->length();i++){
                QString temp = fixedStringList->at(i);
                if(i != fixedStringList->length() - 1)
                    saveStream << temp << "\n";
                    else {

                        saveStream << temp;
                    }
            }
        }
        fixedFile.close();
        delete fixedStringList;
        fixedStringList = nullptr;
    }
}
